#include "algorithm.h"
// #define __RIGHT_MOVE

// insert a new value into source array, move the whole elements in array same time
void array_pos_insert(int *source, int len, int pos, int value)
{
    // if position out of index, there is not neccessary to migration
    if (pos > len)
        return;

#ifdef __RIGHT_MOVE // right
    // traversal from end of array to pos(middle), move to right,cover last element when migration
    for (int index = len - 1; index >= pos; index--)
        source[index + 1] = source[index];
#else // or left
    // traversal from front to pos, move them to left, cover first element when migration
    for (int index = 0; index <= pos; index++)
        source[index] = source[index + 1];
#endif
    // put the new value into rigth position after migration
    source[pos] = value;
}